package com.example.testdemoutils.dynamic_data_source.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.testdemoutils.dynamic_data_source.aop.DbKey;
import com.example.testdemoutils.dynamic_data_source.aop.DynamicDataSource;
import com.example.testdemoutils.dynamic_data_source.po.Student;
import com.example.testdemoutils.dynamic_data_source.po.SysUserEntity;
import com.example.testdemoutils.dynamic_data_source.po.Teacher;
import com.example.testdemoutils.dynamic_data_source.service.DatasourceService;
import com.example.testdemoutils.dynamic_data_source.service.StudentService;
import com.example.testdemoutils.dynamic_data_source.service.TeacherService;
import com.example.testdemoutils.dynamic_data_source.service.impl.DynamicDataSourceTestService;
import com.example.testdemoutils.dynamic_data_source.utils.R;
import com.example.testdemoutils.dynamic_data_source.utils.ResultMsg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/base")
public class BaseController {

    /**
     * 查看数据源的所有表列表
     *
     * @param id
     * @return
     */
//    @RequestMapping("/getTableList/{id}")
//    public ResultMsg getTableList(@PathVariable("id") Integer id) {
//        QueryWrapper<DynamicDataSourceModel> objectQueryWrapper = new QueryWrapper<>();
//        objectQueryWrapper.eq("id", id);
//        List<DynamicDataSourceModel> list = dataSourceService.list(objectQueryWrapper);
//        return ResultMsg.ok(list);
//    }

    @Resource
    StudentService studentService;
    @Resource
    TeacherService teacherService;
    @Resource
    DynamicDataSourceTestService dynamicDataSourceTestService;
    @Resource
    DatasourceService datasourceService;

    @PostMapping("/addStudentAndTeacher")
    public ResultMsg addStudentAndTeacher(String studentName, String teacherName) {
        //新增一个学生
        Student student = new Student();
        student.setName(studentName);
        studentService.save(student);
        //新增一个教师
        Teacher teacher = new Teacher();
        teacher.setName(teacherName);
        teacherService.save(teacher);
        return ResultMsg.ok();
    }

    @RequestMapping("/find")
    public ResultMsg find() {
        //查询student表所有的学生
        List<Student> studentList = studentService.list();
        //查询teacher表所有的教师
        List<Teacher> teacherList = teacherService.list();
        JSONObject data = new JSONObject();
        data.put("studentList", studentList);
        data.put("teacherList", teacherList);
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setData(data);
        return resultMsg;
    }

    @GetMapping("update")
    public ResultMsg update() {
        dynamicDataSourceTestService.updateUser(1L);
        dynamicDataSourceTestService.updateUserBySlave1(1L);
        dynamicDataSourceTestService.updateUserBySlave2(1L);
        return ResultMsg.ok();
    }

    /**
     * 查看数据源的所有表列表
     *
     * @param dbKey @return
     */
    @DynamicDataSource
    @RequestMapping("/getTableList/{dbKey}/{userName}")
    public R getTableList(@PathVariable("dbKey") @DbKey String dbKey, @PathVariable("userName") String userName) throws Exception {
        SysUserEntity sysUserEntity = datasourceService.queryTableList(dbKey, userName);
        return R.ok().put("data", sysUserEntity);
    }


}
